//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MakeSchedule.h"
using namespace Cephei::QL::Times;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/Period.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::CMakeSchedule::CMakeSchedule () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMakeSchedule = NULL;
#endif
        _ppMakeSchedule = new boost::shared_ptr<QuantLib::MakeSchedule> (new QuantLib::MakeSchedule ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (boost::shared_ptr<QuantLib::MakeSchedule>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSchedule = NULL;
#endif
	_ppMakeSchedule = &childNative;
    
}
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (QuantLib::MakeSchedule& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMakeSchedule = NULL;
#endif
	_ppMakeSchedule = new boost::shared_ptr<QuantLib::MakeSchedule> (&childNative);
    
    _MakeScheduleOwner = owner;
    
}

Cephei::QL::Times::CMakeSchedule::CMakeSchedule (CMakeSchedule^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSchedule = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMakeSchedule = new boost::shared_ptr<QuantLib::MakeSchedule> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSchedule = NULL;
#endif
	if (!t->IsSubclassOf(CMakeSchedule::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (QuantLib::Handle<QuantLib::MakeSchedule>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeSchedule = &childNative;
	_ppMakeSchedule = &static_cast<boost::shared_ptr<QuantLib::MakeSchedule>>(childNative.currentLink());
    
    _MakeScheduleOwner = owner;
}
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (QuantLib::Handle<QuantLib::MakeSchedule> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeSchedule = &childNative;
	_ppMakeSchedule = &static_cast<boost::shared_ptr<QuantLib::MakeSchedule>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CMakeSchedule::CMakeSchedule (QuantLib::MakeSchedule childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSchedule = NULL;
#endif
	_ppMakeSchedule = new boost::shared_ptr<QuantLib::MakeSchedule> (new QuantLib::MakeSchedule (childNative));
    
}
#endif

Cephei::QL::Times::CMakeSchedule::~CMakeSchedule ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMakeSchedule != NULL)
    {
	    delete _ppMakeSchedule;
        _ppMakeSchedule = NULL;
    }
}
Cephei::QL::Times::CMakeSchedule::!CMakeSchedule ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMakeSchedule != NULL)
    {
	    delete _ppMakeSchedule;
    }
}
QuantLib::MakeSchedule& Cephei::QL::Times::CMakeSchedule::GetReference ()
{
    if (_ppMakeSchedule == NULL) throw gcnew NativeNullException ();
	return **_ppMakeSchedule;
}
boost::shared_ptr<QuantLib::MakeSchedule>& Cephei::QL::Times::CMakeSchedule::GetShared ()
{
    if (_ppMakeSchedule == NULL) throw gcnew NativeNullException ();
	return *_ppMakeSchedule;
}
QuantLib::MakeSchedule* Cephei::QL::Times::CMakeSchedule::GetPointer ()
{
    if (_ppMakeSchedule == NULL) throw gcnew NativeNullException ();
	return &**_ppMakeSchedule;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MakeSchedule>& Cephei::QL::Times::CMakeSchedule::GetHandle ()
{
	if (_phMakeSchedule == NULL)
	{
		_phMakeSchedule = new Handle<QuantLib::MakeSchedule> (*_ppMakeSchedule);
	}
	return *_phMakeSchedule;
}
#endif
bool Cephei::QL::Times::CMakeSchedule::HasNative () 
{
	return (_ppMakeSchedule != NULL);
}

Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::From (DateTime effectiveDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _effectiveDate = (QuantLib::Date)ValueHelper::Convert (effectiveDate);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->from ( _effectiveDate );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::Backwards::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->backwards ( );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::EndOfMonth (Microsoft::FSharp::Core::FSharpOption<Boolean>^ flag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        bool _flag = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (flag) ? (bool)ValueHelper::Convert (flag->Value) : true); //9a
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->endOfMonth ( _flag );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::Forwards::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->forwards ( );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithFirstDate (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withFirstDate ( _d );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithNextToLastDate (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withNextToLastDate ( _d );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithRule (QL::Times::DateGeneration::RuleEnum r)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::DateGeneration::Rule _r = (QuantLib::DateGeneration::Rule)r ;
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withRule ( _r );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithTerminationDateConvention (QL::Times::BusinessDayConventionEnum conv)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _conv = (QuantLib::BusinessDayConvention)conv ;
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withTerminationDateConvention ( _conv );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::To (DateTime terminationDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _terminationDate = (QuantLib::Date)ValueHelper::Convert (terminationDate);
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->to ( _terminationDate );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithCalendar (Cephei::QL::Times::ICalendar^ calendar)
{
    CCalendar^ _Ccalendar;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withCalendar ( _calendar );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithConvention (QL::Times::BusinessDayConventionEnum conv)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _conv = (QuantLib::BusinessDayConvention)conv ;
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withConvention ( _conv );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithFrequency (QL::Times::FrequencyEnum frequency)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withFrequency ( _frequency );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule::WithTenor (Cephei::QL::Times::IPeriod^ tenor)
{
    CPeriod^ _Ctenor;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
    	QuantLib::MakeSchedule& _rv = (QuantLib::MakeSchedule&)(*_ppMakeSchedule)->withTenor ( _tenor );   
        Cephei::QL::Times::CMakeSchedule^ _nrv = gcnew Cephei::QL::Times::CMakeSchedule (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::IMakeSchedule^ Cephei::QL::Times::CMakeSchedule_Factory::Create ()
{
    return gcnew CMakeSchedule ();
}
